package com.sangmo.fts.api.factoring;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.data.filter.ValueFilter;
import com.sangmo.boot.framework.data.json.ExtViewHelper;
import com.sangmo.boot.framework.plugin.dataview.data.DataViewPageSearch;
import com.sangmo.fts.basic.model.Company;
import com.sangmo.fts.process.factoring.interfaces.FfWaybillCreation;
import com.sangmo.fts.process.factoring.interfaces.FfWaybillReceiveRequest;
import com.sangmo.fts.process.factoring.model.FfWaybill;
import com.sangmo.fts.process.factoring.model.FfWaybillItem;
import com.sangmo.fts.process.factoring.service.FfWaybillService;
import com.sangmo.fts.process.receivable.interfaces.RpfWaybillCreation;
import com.sangmo.fts.process.receivable.interfaces.RpfWaybillReceiveRequest;
import com.sangmo.fts.restructure.meta.TrainingApi;
import com.sangmo.fts.restructure.meta.TrainingProcess;
import com.sangmo.fts.training.model.TrainingHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;

@TrainingApi
@Controller
@RequestMapping("/ffWaybill/v1")
@Access("ffWaybill")
public class FfWaybillApi  {

    @Autowired
    private FfWaybillService ffWaybillService;

    @Access(":create")
    @PostMapping(value = "/{trainingId}/create")
    public @ResponseBody
    ApiResponse<FfWaybill> create(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid FfWaybillCreation obj) {
        obj.setTrainingId(trainingId);
        return ApiResponse.make(ffWaybillService.create(obj));
    }

    @Access(":remove")
    @PostMapping("/{trainingId}/{id}/remove")
    @ResponseBody
    public  ApiResponse remove(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ffWaybillService.deleteById(id);
        return ApiResponse.make();
    }

    @Access(":read")
    @PostMapping("/{trainingId}/search")
    @ResponseBody
    public  ApiResponse<PageResult<FfWaybill>> search(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("FfWaybill.search", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(ffWaybillService.search(ps));
    }

    @TrainingProcess(node = TrainingHistory.FfProcessNodes.FF_WAYBILL_SELLER_SHIP_3)
    @Access(":ship")
    @PostMapping("/{trainingId}/{id}/ship")
    @ResponseBody
    public  ApiResponse ship(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ffWaybillService.ship(id);
        return ApiResponse.make();
    }

    @Access(":read")
    @PostMapping("/{trainingId}/{id}/items/list")
    @ResponseBody
    public  ApiResponse<PageResult<FfWaybillItem>> listItemsByPage(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long waybillId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        return ApiResponse.make(ffWaybillService.listItemsByPage(waybillId, ps));
    }

    @TrainingProcess(node = TrainingHistory.FfProcessNodes.FF_WAYBILL_BUYER_RECEIVE_4)
    @Access(":receive")
    @PostMapping("/{trainingId}/{id}/receive")
    @ResponseBody
    public  ApiResponse receive(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id, @RequestBody @Valid FfWaybillReceiveRequest request) {
        ffWaybillService.receive(id, request);
        return ApiResponse.make();
    }

    @Access(":selection")
    @PostMapping("/{trainingId}/selection")
    public @ResponseBody ApiResponse<PageResult<FfWaybill>> selection(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("FfWaybill.selection", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(ffWaybillService.selection(ps));
    }

    @Access(":sellerSelection")
    @PostMapping("/{trainingId}/seller/selection")
    public @ResponseBody ApiResponse<PageResult<Company>> sellerSelection(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("FfWaybill.sellerSelection", 1);
//        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(ffWaybillService.sellerSelection(trainingId, ps));
    }


}
